package org.dromara.application.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.application.domain.vo.AppUserVo;
import org.dromara.application.domain.bo.AppUserBo;
import org.dromara.application.service.IAppUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 应用和用户关联
 *
 * @author YCJZ
 * @date 2025-09-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/application/user")
public class AppUserController extends BaseController {

    private final IAppUserService appUserService;

    /**
     * 查询应用和用户关联列表
     */
    @SaCheckPermission("application:user:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserVo> list(AppUserBo bo, PageQuery pageQuery) {
        return appUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出应用和用户关联列表
     */
    @SaCheckPermission("application:user:export")
    @Log(title = "应用和用户关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserBo bo, HttpServletResponse response) {
        List<AppUserVo> list = appUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "应用和用户关联", AppUserVo.class, response);
    }

    /**
     * 获取应用和用户关联详细信息
     *
     * @param clientId 主键
     */
    @SaCheckPermission("application:user:query")
    @GetMapping("/{clientId}")
    public R<AppUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String clientId) {
        return R.ok(appUserService.queryById(clientId));
    }

    /**
     * 新增应用和用户关联
     */
    @SaCheckPermission("application:user:add")
    @Log(title = "应用和用户关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(appUserService.insertByBo(bo));
    }

    /**
     * 修改应用和用户关联
     */
    @SaCheckPermission("application:user:edit")
    @Log(title = "应用和用户关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(appUserService.updateByBo(bo));
    }

    /**
     * 删除应用和用户关联
     *
     * @param clientIds 主键串
     */
    @SaCheckPermission("application:user:remove")
    @Log(title = "应用和用户关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clientIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] clientIds) {
        return toAjax(appUserService.deleteWithValidByIds(List.of(clientIds), true));
    }
}
